package com.kevin.demo.springbootdemo2.manager;

import com.kevin.demo.springbootdemo2.mapper.UserMapper;
import com.kevin.demo.springbootdemo2.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author kevin
 * @version： UserManager.java v 1.0, 2020年05月20日 17:43
 * @Description
 **/
@Component
@Transactional(rollbackFor = {Exception.class})
public class UserManager {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户列表，并通过 @Cacheable 注解 将结果数据放入缓存
     *
     * @return
     */
    @Cacheable(value = "userCache", key = "'user.queryUsers'")
    public List<User> queryUsers() {
        return userMapper.queryUsers();
    }

    /**
     * 删除用户，同时删除对应的缓存
     *
     * @param id
     * @return
     */
    @CacheEvict(value = "userCache", key = "'user.queryUsers'")
    public int deleteUser(int id) {
        int i = userMapper.deleteUser(id);
        return i;
    }
}
